<?php

namespace catchAdmin\phone\controller;

use catchAdmin\lm\model\Address;
use catchAdmin\lm\model\Bbs;
use catchAdmin\lm\model\Cardinfo;
use catchAdmin\lm\model\Dy;
use catchAdmin\lm\model\Goodsinfo;
use catchAdmin\lm\model\Jianyan;
use catchAdmin\lm\model\Lvzhi;
use catchAdmin\lm\model\Taizhang;
use catchAdmin\lm\model\Zuzhi;
use catchAdmin\permissions\model\Users as U;
use catchAdmin\phone\common\Jwt;
use catchAdmin\phone\model\Wxuser;
use catcher\CatchResponse;
use catcher\exceptions\FailedException;
use think\facade\Db;
use think\facade\Request;


class User extends Jwt
{

    public function userinfo()
    {
        $user = Db::name('wxuser')->where('openid', $this->openid)->find();

        if (!$user || $user['status'] == 0) {
            return CatchResponse::success(205, 205, 200);
        }

        return CatchResponse::success([
            'token' => $user,
            'name' => Wxuser::where('openid', $this->openid)->find()['nickname'],
            'tz' => Taizhang::where('openid', $this->openid)->count()
        ], 200);

//        $sf = Db::name('shop')->where('openid', $this->openid)->find(); //门店
//        $daili = Db::name('shidai')->where('openid', $this->openid)->find();
//        $vrsuer = Db::name('vruser')->where('openid', $this->openid)->find();
//
//        if (!empty($daili) && !empty($sf)) {
//            return CatchResponse::success($user, 203);   //门店+ 市级代理
//        }
//
//        if (!empty($sf)) {
//            return CatchResponse::success($user, 201);//门店
//        }
//        if (!empty($daili)) {
//            return CatchResponse::success($user, 202);
//        }
//        if (!empty($vrsuer)) {
//            return CatchResponse::success($user, 204);   //普通用户
//        }
//        return CatchResponse::success([
//            'token' => $user,
//        ],205);

    }


    public function usershenhe()
    {
        $data = Request::param('data');
        $address = Request::param('address');
        $info = Cardinfo::where(['name' => $data['name'], 'phone' => $data['phone'], 'status' => 0])->find();
        if (!$info) {
            throw new FailedException("用户信息不正确或者该信息已经注册使用!,请检查后输入");
        }
        Cardinfo::where('id',$info['id'])->update([
            'status'=>1
        ]);
        $cun = Address::find($address['id'])->toArray();
        $xiang = Address::where('id', $cun['parent_id'])->find()->toArray();
        // 绑定身份的时候写入creatro_id 用户绑定身份到对应的县级权限
        $name = Address::find(Address::find($data['address'])['parent_id'])['id'];
        Wxuser::where('openid', $this->openid)->update([
            'status' => 1,
            'nickname' => $data['name'],
            'phone' => $data['phone'],
            'cun' => $address['id'],
            'xiang' => $xiang['name'],
            'qu' => Address::where('id', $xiang['parent_id'])->value('name'),
            'cunid' => $data['address'],
            'creator_id' => U::where('username', Address::find($name)['name'])->find()['id']
        ]);

//        $db = new Executive;
//
//        $db->name = $data['name'];
//        $db->phone = $data['phone'];
//        $db->card = $data['card'];
//        $db->status = 1;
//        $db->save();

        Db::name('Executive')->insert([
            'name' => $data['name'],
            'phone' => $data['phone'],
//            'card' => $data['card'],
            'status' => 1]);

        return CatchResponse::success([
            'message' => "绑定完成",
        ], 205);


    }

    public function mylist()
    {
        return CatchResponse::success(\catchAdmin\lm\model\Goods::where('openid', $this->openid)->select());
    }

    public function addresslist()
    {
        $ids = Address::where('parent_id', 0)->column('id');
        $id = Address::wherein('parent_id', $ids)->column('id');
        $s = array_merge($ids, $id);
        return CatchResponse::success(Address::field(['name as text,id,parent_id,id as value'])->wherein('id', $s)->select()->toTree());
    }

    /**
     * 获取自己的项目 跟踪项目进度
     */

    public function myxiangmu($id)
    {
        $info[1] = \catchAdmin\lm\model\Goods::where('id', $id)->find();
        if ($info[1]['openid'] != $this->openid) {
            return CatchResponse::success('error', 1, 201);
        }
        $info[2] = Goodsinfo::where('goodsid', $id)->order('id', 'desc')->select()->toArray();
        return CatchResponse::success($info);
    }

    /**
     * 提交信息
     */

    public function tjtext()
    {
        $data = Request::param();
        $data = Goodsinfo::insert([
            'address' => $data['data']['address'],
            'people' => $data['data']['people'],
            'time' => $data['data']['time'],
            'text' => $data['data']['text'],
            'goodsid' => $data['id'],
            'openid' => $this->openid,
            'image1' => $data['data']['image'][0] ?? '',
            'image2' => $data['data']['image'][1] ?? '',
            'image3' => $data['data']['image'][2] ?? '',
            'zwname' => Wxuser::where('openid', $this->openid)->find()['nickname']
        ]);
        if ($data) {
            return CatchResponse::success($data);
        }
        return 1;
    }


    public function tjbbs()
    {
        Bbs::insert([
            'title' => Request::param()['name'],
            'neirong' => Request::param()['text'],
            'zwname' => Wxuser::where('openid', $this->openid)->find()['nickname'],
            'created_at' => time(),
            'openid' => $this->openid
        ]);
        return CatchResponse::success('ok');
    }

    /*
     * bbslist
     */

    public function bbslist($page)
    {
        $data = Bbs::limit($page)->order('id', 'desc')->select()->toArray();
        foreach ($data as $k => &$v) {
            if ($v['openid'] == $this->openid) {
                $v['del'] = 1;
            }
        }
        return CatchResponse::success($data);
    }

    /**
     * 删除论坛
     */

    public function delbbsbyid($id)
    {
        return CatchResponse::success(Bbs::destroy($id));
    }

    /**
     * 提交调研
     */

    public function tjdy()
    {
        $data = Request::param();
        Dy::insert([
            'title' => $data['title'],
            'time' => $data['time'],
            'neirong' => $data['neirong'],
            'created_at' => time(),
            'address' => $data['address'],
            'zwname' => Wxuser::where('openid', $this->openid)->find()['nickname'],
            'openid' => $this->openid
        ]);
        return CatchResponse::success('ok');
    }

    /**
     * 提交建言
     */

    public function tjjy()
    {
        $data = Request::param();
        Jianyan::insert([
            'title' => $data['title'],
//            'time'=>$data['time'],
            'neirong' => $data['neirong'],
            'created_at' => time(),
            'lingyu' => $data['lingyu'],
            'zwname' => Wxuser::where('openid', $this->openid)->find()['nickname'],
            'openid' => $this->openid
        ]);
        return CatchResponse::success('ok');
    }

    /**
     * 汇总台账数量
     */

    public function mytznum()
    {
        $num = Taizhang::where('openid', $this->openid)->count();
        $zz = Zuzhi::where('openid', $this->openid)->count();
        if ($num > 9 && $zz) {
            return CatchResponse::success('11');
        }
        if ($num > 9 && empty($zz)) {
            return CatchResponse::success('10');
        }
        return CatchResponse::success($num);


    }

    /**
     *
     * 添加台账
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @author 张智先生
     * @date 2023-05-22 21:55
     */
    public function addtaizhang()
    {

        if (Taizhang::where('openid', $this->openid)->count() > 9) {
            return CatchResponse::success('ok', 301, 301);
        }
        $data = Request::param();
        Taizhang::insert([
            'name' => $data['name'],
            'danwei' => $data['danwei'],
            'age' => $data['age'],
            'phone' => $data['phone'],
            'zhiwu' => $data['zhiwu'],
            'sex' => $data['sex'] == 0 ? '男' : '女',
            'created_at' => time(),
//            'lingyu'=>$data['lingyu'],
            'zwname' => Wxuser::where('openid', $this->openid)->find()['nickname'],
            'openid' => $this->openid,
            'creator_id' => Wxuser::where('openid', $this->openid)->find()['creator_id']
        ]);
        return CatchResponse::success('ok');
    }


    /*
     * 提交履职情况表单
     * @return
     */

    public function sendlvzhi()
    {

        $data = \request()->post();
        $yearData = Lvzhi::where('openid', $this->openid)->whereYear('created_at', date('Y'))->find();
        $datas = Wxuser::where('openid', $this->openid)->find();
        $data['zwname'] = $datas['nickname'];
        $data['zwphone'] = $datas['phone'];
        $data['openid'] = $this->openid;
        $data['creator_id'] = $datas['creator_id'];
        if (!empty($yearData)) {
//            $data['id'] = $yearData;
            return CatchResponse::success((new Lvzhi())->updateBy($yearData['id'], $data));
        }
        (new Lvzhi())->storeBy($data);
        return CatchResponse::success('ok');
    }


    /**
     * 履职页面登陆判断
     */

    public function getlvzhiinfo()
    {
        $yearData = Lvzhi::where('openid', $this->openid)->whereYear('created_at', date('Y'))->select()->toArray();
        if (!empty($yearData)) {
            return CatchResponse::success($yearData, 401, 401);
        } else {
            return CatchResponse::success('ok');
        }
    }


    /**
     *
     *
     * @author 张智先生
     * @date 2023-05-13 16:23
     */
    public function addzzinfo()
    {
        if (Zuzhi::where('openid', $this->openid)->find()) {
            return CatchResponse::success('ok', 301, 301);
        }
        $data = Request::param();
        Zuzhi::insert([
            'zzname' => $data['zzname'],
            'zzfzrname' => $data['zzfzrname'],
            'zzlxphone' => $data['zzlxphone'],
            'zzaddress' => $data['zzaddress'],
            'created_at' => time(),
            'zwname' => Wxuser::where('openid', $this->openid)->find()['nickname'],
            'zzphone' => Wxuser::where('openid', $this->openid)->find()['phone'],
            'openid' => $this->openid,
            'creator_id' => Wxuser::where('openid', $this->openid)->find()['creator_id']
        ]);
        return CatchResponse::success('ok');
    }


    /**
     *
     * 我的台账信息
     * @author 张智先生
     * @date 2023-05-15 16:49
     */
    public function mytzinfo()
    {
        $tzinfo[1] = Taizhang::where('openid', $this->openid)->order('id', 'desc')->select();
        $tzinfo[2] = Zuzhi::where('openid', $this->openid)->order('id', 'desc')->find();
        return CatchResponse::success($tzinfo);
    }

    public function getthree($id)
    {
        return CatchResponse::success(Address::where('parent_id', $id)->column('id,name'));
    }

}